package com.jzy.service.impl;

import com.jzy.entity.Vo.CommentVo;
import com.jzy.entity.Vo.ContentVo;
import com.jzy.exception.TipException;
import com.jzy.repository.CommentRepository;
import com.jzy.service.ICommentService;
import com.jzy.service.IContentService;
import com.jzy.util.DateKit;
import com.jzy.util.TaleUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;

@Service
public class CommentServiceImpl implements ICommentService {

    @Resource
    private CommentRepository commentRepository;

    @Resource
    private IContentService contentService;

    @Override
    public Page<CommentVo> getComments(Integer cid, int page, int limit) {
        if (null != cid) {
            Specification<CommentVo> specification = (Specification<CommentVo>) (root, query, criteriaBuilder) -> {
                ArrayList<Predicate> predicates = new ArrayList<>();
                predicates.add(criteriaBuilder.equal(root.get("cid"), cid));
                predicates.add(criteriaBuilder.equal(root.get("parent"), 0));
                int size = predicates.size();
                return criteriaBuilder.and(predicates.toArray(new Predicate[size]));
            };
            ArrayList<Sort.Order> orders = new ArrayList<>();
            orders.add(new Sort.Order(Sort.Direction.DESC, "coid"));
            Sort by = Sort.by(orders);
            PageRequest of = PageRequest.of(page - 1, limit, by);
            return commentRepository.findAll(specification, of);
        }
        return null;
    }

    @Override
    public void insertComment(CommentVo comments) {
        if (null == comments) {
            throw new TipException("评论对象为空");
        }
        if (StringUtils.isBlank(comments.getAuthor())) {
            comments.setAuthor("热心网友");
        }
        if (StringUtils.isNotBlank(comments.getMail()) && !TaleUtils.isEmail(comments.getMail())) {
            throw new TipException("请输入正确的邮箱格式");
        }
        if (StringUtils.isBlank(comments.getContent())) {
            throw new TipException("评论内容不能为空");
        }
        if (comments.getContent().length() < 5 || comments.getContent().length() > 2000) {
            throw new TipException("评论字数在5-2000个字符");
        }
        if (null == comments.getCid()) {
            throw new TipException("评论文章不能为空");
        }
        ContentVo contents = contentService.getContents(String.valueOf(comments.getCid()));
        if (null == contents) {
            throw new TipException("不存在的文章");
        }
        comments.setOwnerId(contents.getAuthorId());
        comments.setCreated(DateKit.getCurrentUnixTime());
        commentRepository.saveAndFlush(comments);
        ContentVo temp = new ContentVo();
        temp.setCid(contents.getCid());
        temp.setCommentsNum(contents.getCommentsNum() + 1);
        contentService.updateContentByCid(temp);
    }
}
